﻿using FileService.Domain;
using FileService.Domain.Entities;
using Kogel.Repository;
using Minio.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileService.infrastructure
{
    public class TabRepository : ITabRepository
    {

        private readonly IRepository<TimeTab> repository;
        public TabRepository(IRepository<TimeTab> repository)
        {
            this.repository = repository;
        }

        public Task<int> AddAsync<T>(T item)
        {
          return  repository.CommandSet<T>().InsertAsync(item);
        }

        public Task<int> DeleteAsync<T>(T item)
        {
            return repository.CommandSet<T>().DeleteAsync(item);
        }

        public Task<T> FindByIdAsync<T>(object id)
        {
           return repository.QuerySet<T>().GetRepository().FindByIdAsync(id);
        }

        public Task<TimeTab> FindTimeTabByNameAsync(string name)
        {
            return repository.QuerySet().Where(x=>x.Name==name).GetAsync();
        }

        public Task<int> UpdateAsync<T>(T entity)
        {
           return repository.CommandSet<T>().UpdateAsync(entity);
        }

        public Task<IEnumerable<T>> QueryListByParentIdAsync<T>(long id) where T : class, IParentId
        {
           return repository.QuerySet<T>().Where(x=>x.ParentId==id).ToIEnumerableAsync();  
        }

        public Task<IEnumerable<TimeTab>> QueryTimeTabAsync()
        {
            return repository.QuerySet().OrderByDescing(x => x.Time).ToIEnumerableAsync();
        }

        public Task<int> UpdateAsync<T>(IEnumerable<T> entities)
        {
            return repository.CommandSet<T>().UpdateAsync(entities);
        }
    }
}
